<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/13
 * Time: 23:51
 */

namespace app\common\model;

use app\common\cachemodel\GoddessArea;
use app\common\cachemodel\ZlGoddessState;
use XznsTools\cache\CacheManage;

/**
 * 文章点赞模型
 * Class ArticleGood
 * @package app\common\mode
 */
class ArticleGood extends BaseCache
{
    protected $table = 'wechatadmin.system_article_good';
    protected $cacheModel = 'content.content_good';

    /**
     * 添加点赞
     * @param array $userInfo
     * @param int $id 内容id
     * @param array $data
     * @param int $type 类型 1=>星文，2=>女神动态，3=>女神专区
     * @return bool
     */
    public function saveData($userInfo, int $id, array $data, int $type)
    {
        try
        {
            //获取对应的内容信息
            switch ($type)
            {
                case 1:
                    $article = current((new \app\common\cachemodel\Article())->batchGetInfo([$id], ['id', 'title']));
                    break;
                case 2:
                    $article = (new ZlGoddessState())->getInfoById($id, ['id']);
                    break;
                case 3:
                    $article = (new GoddessArea())->batchGetThemeInfo([$id], ['id', 'reply_avatars']);
                    break;
                default:
                    $article = [];
                    break;
            }
            if ($article)
            {
                $this->save($data);
                $this->listDataAdd((int)$userInfo['id'], [$id => $id], $type);
                if ($type === 3 && $userInfo['avatar'])
                {   // 互动头像添加
                    (new GoddessArea())->interactionAvatars($id, $userInfo['avatar'], $article);
                }
                $cacheKey = $type == 1 ? 'good' : 'good_num';
                //对应内容点赞数据自增 1
                (new \app\common\cachemodel\ArticleComment())->commentOrgoodAdd($type, $id, $cacheKey, 1);
            }
        }
        catch (\Exception $e)
        {
            return false;
        }
        return true;
    }

    /**
     * 取消点赞
     * @param int $uid
     * @param int $id 内容id
     * @param array $where
     * @param int $type 类型 1=>星文，2=>女神动态，3=>女神专区
     * @return bool
     */
    public function delData(int $uid, int $id, array $where, int $type)
    {
        try
        {
            //获取对应的内容信息
            switch ($type)
            {
                case 1:
                    $article = current((new \app\common\cachemodel\Article())->batchGetInfo([$id], ['id', 'title']));
                    break;
                case 2:
                    $article = (new ZlGoddessState())->getInfoById($id);
                    break;
                case 3:
                    $article = (new GoddessArea())->batchGetThemeInfo([$id], ['id']);
                    break;
                default:
                    $article = [];
                    break;
            }
            if ($article)
            {
                $this->where($where)->delete();
                $this->listDataDel($uid, [$id], $type);
                //对应内容点赞数据自增 -1
                $commentKey = $type == 1 ? 'good' : 'good_num';
                (new \app\common\cachemodel\ArticleComment())->commentOrgoodAdd($type, $id, $commentKey, -1);
            }
        }
        catch (\Exception $e)
        {
            return false;
        }
        return true;
    }

    /**
     * 获取用户点赞的id集合，用于判断是否点赞
     * @param int $uid
     * @param int $type 类型 1=>星文，2=>女神动态，3=>女神专区
     * @return array
     * @throws \Exception
     */
    public function getGoodFromCache(int $uid, int $type)
    {
        $aids = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->setListKeyType($type)->ZsGetIocData('list', function () use ($uid, $type)
        {
            return $this->getGoodFromDms($uid, $type);
        });
        return $aids;
    }

    /**
     * 获取用户点赞的id集合，用于判断是否点赞
     * @param int $uid
     * @param int $type 类型 1=>星文，2=>女神动态，3=>女神专区
     * @return array
     */
    public function getGoodFromDms(int $uid, int $type)
    {
        try
        {
            $aids = $this->where(['uid' => $uid, 'type' => $type])->column('aid');
            $aids = keyValue($aids);
        }
        catch (\Exception $e)
        {
            $aids = [];
        }
        return $aids;
    }

    /**
     * 关联文章
     * @return \think\model\relation\belongsTo
     */
    public function article()
    {
        return $this->belongsTo('Article', 'id', 'aid');
    }
}
